﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Xml.Serialization;



namespace ILX.Portal.Web
{
    public partial class _Default : System.Web.UI.Page
    {

        public List<DDLDataType> GetOption(string ddlType)
        {
            List<DDLDataType> tags = null;

            switch (ddlType)
            {
                case "FormType":
                    tags = new List<DDLDataType>()
                {
                    new DDLDataType("Approval"), new DDLDataType("Input")
                };
                    break;
                case "TaskType":
                    tags = new List<DDLDataType>()
                {
                    new DDLDataType("Serial"), new DDLDataType("Parallel")
                };
                    break;
                case "ReSubmit":
                    tags = new List<DDLDataType>()
                {
                    new DDLDataType("Yes"), new DDLDataType("No")
                };
                    break;
            }


              

          
            return tags;// GetTable();
        }
        private DataTable GetTable()
        {
            //
            // Here we create a DataTable with four columns.
            //
            DataTable table = new DataTable();
            table.Columns.Add("Level", typeof(string));
            table.Columns.Add("FirstName", typeof(string));
            table.Columns.Add("DueDays", typeof(string));
            table.Columns.Add("TotalResource", typeof(string));
            table.Columns.Add("LastName", typeof(string));
            table.Columns.Add("FormType", typeof(string));
            table.Columns.Add("ReSubmit", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));

            //
            // Here we add five DataRows.
            //
            table.Rows.Add(1, "Indocin", "1","2", "Serial", "Input","Yes", DateTime.Now);
            table.Rows.Add(2, "Enebrel", "1", "1", "Serial", "Input", "Yes", DateTime.Now);
            table.Rows.Add(3, "Hydralazine", "4", "1", "Serial", "Approval", "No", DateTime.Now);
            table.Rows.Add(4, "Combivent", "10", "2", "Parallel", "Approval", "No", DateTime.Now);
            table.Rows.Add(5, "Dilantin", "1", "2", "Parallel", "Approval", "Yes", DateTime.Now);
            return table;
        }
       
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                //Test Records  
                GridView1.DataSource = GetTable();
                GridView1.DataBind();

              
            }
        }
        protected void MoveGridViewRows(object sender, EventArgs e)
        {
            DataTable dtbl = new DataTable();
            int indx = -1;

            ImageButton btnUp = (ImageButton)sender;
            GridViewRow row = (GridViewRow)btnUp.NamingContainer;

            // Get all items except the one selected  

            var rows = GridView1.Rows.Cast<GridViewRow>().Where(a => (a != row || btnUp.CommandName=="Insert")).ToList();


            switch (btnUp.CommandName)
            {
                case "Insert":
                    //If First Item, insert at end (rotating positions)  

                    indx = row.RowIndex + 1;
                    rows.Insert(indx, row);
                    break;
                case "Up":
                    //If First Item, insert at end (rotating positions)  
                    if (row.RowIndex.Equals(0))
                        rows.Add(row);
                    else
                        rows.Insert(row.RowIndex - 1, row);
                    break;
                case "Down":
                    //If Last Item, insert at beginning (rotating positions)  
                    if (row.RowIndex.Equals(GridView1.Rows.Count - 1))
                        rows.Insert(0, row);
                    else
                        rows.Insert(row.RowIndex + 1, row);
                    break;
            }


            var ss = rows.Select(a => new
            {

                Level =   ((Label)a.FindControl("Level")).Text,
                FirstName = ((TextBox)a.FindControl("txtFirstName")).Text,
                DueDays = ((TextBox)a.FindControl("txtDueDays")).Text,
                TotalResource = ((TextBox)a.FindControl("txtTotalResource")).Text,
                LastName = ((DropDownList)a.FindControl("dropTaskType")).SelectedValue,
                FormType = ((DropDownList)a.FindControl("dropFormType")).SelectedValue,
                ReSubmit = ((DropDownList)a.FindControl("dropReSubmit")).SelectedValue,
            }).ToList();

            if (btnUp.CommandName == "Insert")
            {
                ss[indx] = null;
            }
            GridView1.DataSource = ss;
            GridView1.DataBind();
        }

        protected void aaa_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void SaveChanges_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            string dat = "" ;
            foreach (GridViewRow row in GridView1.Rows)
            {
            //    dat += ((TextBox)row.FindControl("txtFirstName")).Text;
            }

          //  var row_s = GridView1.Rows.Cast<GridViewRow>().Where(a => (a != row || btnUp.CommandName == "Insert")).ToList();

            var list11 = new List<string> { "a", "b", "a", "c", "a", "b" };


            var q = (GridView1.Rows.Cast<GridViewRow>().GroupBy(x => ((TextBox)x.FindControl("txtFirstName")).Text)
                        .Select(g => new { Value = g.Key, Count = g.Count()})
                        .Where(x => x.Count > 1)
                        .OrderByDescending(x => x.Count));

            foreach (var x in q)
            {
               sb.AppendLine("Value: " + x.Value + " Count: " + x.Count + "<br/>");
            }

            Response.Write(sb.ToString());
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

        }

        protected void OnTaskActivityChange(object sender, EventArgs e)
        {

        }
    }


    public class ProjectMasterType
    {

    }

   public class DDLDataType
    {
       public DDLDataType(string TypeKey)
        {
            this.TypeKey = TypeKey;
            this.TypeValue = TypeKey;
        }

       public DDLDataType(string TypeKey, string TypeValue)
        {
            this.TypeKey = TypeKey;
            this.TypeValue = TypeValue;
        }
        public string TypeKey { get; set; }
        public string TypeValue { get; set; } 
    }
}